﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using enqueteJwVd.Models;
using enqueteJwVd.DAL;
using enqueteJwVd.Services;
using enqueteJwVd.ViewModels;


namespace enqueteJwVd.Controllers
{
    [Authorize(Roles = "e-admin")]
    public class UsersController : Controller
    {
        UnitOfWork unitOfWork = new UnitOfWork();//toevoeging voor unit of work pattern
        IUserService userService;
        //private VDLDbEntities db = new VDLDbEntities();//weggelaten voor repo pattern


        public UsersController(IUserService userService)
        {
            //toevoegening voor repository pattern
           // this.userRepository = new UserRepository(new VDLDbEntities());
            this.userService = userService; //toevoeging voor UnitOfWorkPattern
        }

        public UsersController()
        {
            
        }

        //
        // GET: /Users/

        public ViewResult Index()
        {
            //var user = db.User.Include(u => u.Role);
            //return View(user.ToList());
            var users = userService.GetAllUsers();
            var viewModel = new UserIndexViewModel() { Users = users };
            return View(viewModel);
            //return View(userRepository.GetUsers());
        }

        //
        // GET: /Users/Details/5

        public ViewResult Details(int? id)
        {
            //User user = db.User.Find(id);
            User user = unitOfWork.UserRepository.GetById(id);
            return View(user);
        }

        //
        // GET: /Users/Create

        public ActionResult Create()
        {
            /*var rolIds = from s in unitOfWork.RoleRepository.Get()
                        select s;
             * */
            ViewBag.RolId = new SelectList( unitOfWork.RoleRepository.Get(),"Id", "RolName");
            return View();
        }

        //
        // POST: /Users/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Id")] User user)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.UserRepository.Add(user);
               unitOfWork.SaveChanges();
                //db.User.Add(user);
                //db.SaveChanges();
                return RedirectToAction("Index");
            }

            /*var rolIds = from s in unitOfWork.RoleRepository.Get()
                         select s;
             * */
            ViewBag.RolId = new SelectList(unitOfWork.RoleRepository.Get(), "Id", "RolName", user.RolId);
            //?ViewBag.RolId = new SelectList(db.Role, "RolId", "RolName", user.RolId);
            return View(user);
        }

        //
        // GET: /Users/Edit/5

        public ActionResult Edit(int? id)
        {
            User user = unitOfWork.UserRepository.GetById(id);
            // User user = db.User.Find(id);
            /*var rolIds = from s in unitOfWork.RoleRepository.Get()
                         select s;
             * */
            ViewBag.RolId = new SelectList(unitOfWork.RoleRepository.Get(), "Id", "RolName", user.RolId);
            //?ViewBag.RolId = new SelectList(db.Role, "RolId", "RolName", user.RolId);
            return View(user);
        }

        //
        // POST: /Users/Edit/5

        [HttpPost]
        public ActionResult Edit(User user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    unitOfWork.UserRepository.Update(user);
                    //db.Entry(user).State = EntityState.Modified;
                    unitOfWork.SaveChanges();
                    //db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                //Log the error (add a variable name after DataException)
                ModelState.AddModelError("", "Unable to save changes. Try again and if the proble persists see your system administrator.");
            }
            /*var rolIds = from s in unitOfWork.RoleRepository.Get()
                         select s;
             * */
            ViewBag.RolId = new SelectList(unitOfWork.RoleRepository.Get(), "Id", "RolName", user.RolId);
            //?ViewBag.RolId = new SelectList(db.Role, "RolId", "RolName", user.RolId);
            return View(user);
        }

        //
        // GET: /Users/Delete/5

        public ActionResult Delete(int id, bool? saveChangesError)
        {
            if (saveChangesError.GetValueOrDefault())
            {
                ViewBag.ErrorMessage = "Unable to save changes.";
            }
            User user = unitOfWork.UserRepository.GetById(id);
            //User user = db.User.Find(id);
            return View(user);
        }

        //
        // POST: /Users/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                User user = unitOfWork.UserRepository.GetById(id);
               unitOfWork.UserRepository.Delete(id);
               unitOfWork.SaveChanges();

                //User user = db.User.Find(id);
                //db.User.Remove(user);
                //db.SaveChanges();
            }
            catch (DataException)
            {
                //Log the error
                return RedirectToAction("Delete",
                    new System.Web.Routing.RouteValueDictionary{
                {"id", id},
                {"saveChangesError", true}
            });
            }
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            unitOfWork.Dispose();
            //db.Dispose();
            base.Dispose(disposing);
        }
    }
}